package com.ojoin.ojoin.eb_spider.service.impl;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.Config;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformInfo;

@Service("chrome")
public class ChromeWebDriverServiceimpl extends BasicWebDriverServiceImpl {
	/**
	 * 
	 */

	private static final long serialVersionUID = 1L;

	@Override
	public void initWebDriver() throws Exception {
		initWebDriver(null);
	}

	@Override
	public void initWebDriver(String downloadFilepath) throws Exception {

		WebDriver driver = null;

		DesiredCapabilities cap = init(downloadFilepath);
		String chormePath = Config.getProperty(TbSlPlatformInfo.sl_config,
				"webdriver.chorme.bin");

		String chromedriverPath = Config.getProperty(
				TbSlPlatformInfo.sl_config, "webdriver.chromedriver.bin");

		System.setProperty("webdriver.chrome.driver", chormePath);

		ChromeDriverService service = new ChromeDriverService.Builder()
				.usingDriverExecutable(new File(chromedriverPath))
				.usingAnyFreePort().build();

		service.start();

		driver = new RemoteWebDriver(service.getUrl(), cap);

		this.setDriver(driver);
	}

	public DesiredCapabilities init(String downloadFilepath) {
		ChromeOptions options = new ChromeOptions();
		// String downloadFilepath = "D:\\Supplier_TMALL";
		HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
		chromePrefs.put("profile.default_content_settings.popups", 0);

		if (!CommonUtils.isEmpty(downloadFilepath)) {
			chromePrefs.put("download.default_directory", downloadFilepath);
		}
		HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
		options.setExperimentalOption("prefs", chromePrefs);
		options.addArguments("--test-type");

		DesiredCapabilities cap = DesiredCapabilities.chrome();
		cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
		cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
		cap.setCapability(ChromeOptions.CAPABILITY, options);

		return cap;
	}

	@Override
	public void initRemoteWebDriver(String downloadFilepath) throws Exception {

		WebDriver driver = null;

		DesiredCapabilities cap = init(downloadFilepath);

		String gridHub = "http://"
				+ Config.getProperty(TbSlPlatformInfo.sl_config,
						"webdriver.remote.host") + ":8880/wd/hub";

		driver = new RemoteWebDriver(new URL(gridHub), cap);

		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.manage().window().maximize();

		this.setDriver(driver);

	}

}
